import { ethers } from "hardhat"

describe("mockTest",()=>{
   let VRFCoordinatorV2Mock;
   let MockV3Aggregator;
   let LinkToken;
   let VRFV2Wrapper;
   let RandomNumberDirectFundingConsumerV2;
    it('VRFCoordinatorV2Mock',async()=>{
        const _BASEFEE=BigInt(100000000000000000) 
        const _GASPRICELINK=1000000000
        VRFCoordinatorV2Mock=await ethers.getContractFactory("VRFCoordinatorV2Mock");
        VRFCoordinatorV2Mock=await VRFCoordinatorV2Mock.deploy(_BASEFEE,_GASPRICELINK)
        console.log('VRFCoordinatorV2Mock['+(await VRFCoordinatorV2Mock.getAddress())+']');
    })
    it("MockV3Aggregator",async()=>{
        const _DECIMALS=18;
        const _INITIALANSWER=BigInt(3000000000000000);

        MockV3Aggregator=await ethers.getContractFactory("MockV3Aggregator");
        MockV3Aggregator=await MockV3Aggregator.deploy(_DECIMALS,_INITIALANSWER);
        console.log('MockV3Aggregator['+(await MockV3Aggregator.getAddress())+']');
    })

    it("LinkToken",async()=>{
        LinkToken=await ethers.getContractFactory("LinkToken");
        LinkToken=await LinkToken.deploy();
        console.log('LinkToken['+(await LinkToken.getAddress())+']');
    })

    it("VRFV2Wrapper",async()=>{
        const _LINK =await LinkToken.getAddress();
        const _LINKETHFEED =await MockV3Aggregator.getAddress();
        const _COORDINATOR =await VRFCoordinatorV2Mock.getAddress();
        VRFV2Wrapper=await ethers.getContractFactory("VRFV2Wrapper");
        VRFV2Wrapper=await VRFV2Wrapper.deploy(_LINK,_LINKETHFEED,_COORDINATOR);
        console.log('VRFV2Wrapper['+await VRFV2Wrapper.getAddress()+']');
    })
    
    it("configVRFV2Wrapper",async()=>{
        const _wrapperGasOverhead =60000;
        const _coordinatorGasOverhead=52000;
        const _wrapperPremiumPercentage =10;
        const _keyHash ='0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc';
        const _maxNumWords =10
      await  VRFV2Wrapper.setConfig(_wrapperGasOverhead,_coordinatorGasOverhead,_wrapperPremiumPercentage,_keyHash,_maxNumWords);

    })

    it("Fund the VRFV2Wrapper subscription",async()=>{
        const _subid =1;
        const _amount=BigInt(10000000000000000000);
        
        await VRFCoordinatorV2Mock.fundSubscription(_subid,_amount)
    })

    it("RandomNumberDirectFundingConsumerV2",async()=>{
        const _LINKADDRESS_ =await LinkToken.getAddress();
        const _WRAPPERADDRESS_ =await VRFV2Wrapper.getAddress(); 
        RandomNumberDirectFundingConsumerV2=await ethers.getContractFactory("RandomNumberDirectFundingConsumerV2")
        RandomNumberDirectFundingConsumerV2=await RandomNumberDirectFundingConsumerV2.deploy(_LINKADDRESS_,_WRAPPERADDRESS_)
        console.log('RandomNumberDirectFundingConsumerV2['+await RandomNumberDirectFundingConsumerV2.getAddress()+']');
    })

    it("Fund your VRF consumer contract",async()=>{
        const _to=await RandomNumberDirectFundingConsumerV2.getAddress();
        const _value =BigInt(10000000000000000000);
        await LinkToken.transfer(_to,_value); 
    })
    
    it("Request three random words.",async ()=>{
        const _callbackGasLimit =300000;
        const _requestConfirmations =3;
        const _numWords =1;
        await RandomNumberDirectFundingConsumerV2.requestRandomWords(_callbackGasLimit,_requestConfirmations,_numWords);
        
    })

    it("Fulfill the VRF request",async()=>{
        const _requestId=1;//todo 需要取变量
        const _consumer =await VRFV2Wrapper.getAddress();
        await VRFCoordinatorV2Mock.fulfillRandomWords(_requestId,_consumer);
        
    })

    it("Check the results",async()=>{
       const lastRequestId=await RandomNumberDirectFundingConsumerV2.lastRequestId(); 
        const getRequestStatus =await RandomNumberDirectFundingConsumerV2.getRequestStatus(lastRequestId);
        console.log(getRequestStatus);
    })


})